//
//  UIViewController+Swizzle.m
//  SwizzleMethod
//
//  Created by stevenlfg on 15/11/26.
//  Copyright © 2015年 stevenlfg. All rights reserved.
//

#import "UIViewController+Swizzle.h"
#import "ObjcRuntime.h"
#import "OrderDatailViewController.h"
@implementation UIViewController (Swizzle)
- (void)customViewDidAppear:(BOOL)animated{
    [self customViewDidAppear:animated];
}
- (void)customViewWillAppear:(BOOL)animated{
    if ([[self.navigationController childViewControllers] count]>1) {
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:[self backButton]];
    }
    [self customViewWillAppear:animated];
}
- (void)customViewWillDisappear:(BOOL)animated{
    [self customViewWillDisappear:animated];
}
- (UIButton *)backButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    if ([self isKindOfClass:[OrderDatailViewController class]])
    {
       [button setBackgroundImage:[UIImage imageNamed:@"back_action_white"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"back_action_white"] forState:UIControlStateHighlighted];
    }else
    {
        [button setBackgroundImage:[UIImage imageNamed:@"back_action_ff3064"] forState:UIControlStateNormal];
       [button setBackgroundImage:[UIImage imageNamed:@"back_action_ff3064"] forState:UIControlStateHighlighted];
    }
    button.frame = (CGRect){CGPointZero,button.currentBackgroundImage.size};
    [button addTarget:self action:@selector(goBack_Swizzle) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}
- (void)goBack_Swizzle
{
    [self.navigationController popViewControllerAnimated:YES];
}
+(void)load
{
    swizzleAllViewController();
}
@end
void swizzleAllViewController(){
    Swizzle([UIViewController class], @selector(viewDidAppear:), @selector(customViewDidAppear:));
    Swizzle([UIViewController class], @selector(viewWillAppear:), @selector(customViewWillAppear:));
    Swizzle([UIViewController class], @selector(viewWillDisappear:), @selector(customViewWillDisappear:));
}
